/**
 * Created by yangxianming on 2019/2/1.
 *
 */
function initData(){
    var today = new Date().format("yyyy-MM-dd");
    document.getElementById("date").value = today;
    getReport(today, 1);
    splitPage(getReport);

}

function queryReport(){
    var date = document.getElementById("date").value;
    getReport(date, 1);
}

function queryReportKeyboard(event){
    var e = event || window.event;
    if (e && e.keyCode ==13){
        var date = document.getElementById("date").value;
        getReport(date, 1);
    }
}

function splitPage(pageList){
    //设置分页方法 empListGO
    var listGO = document.getElementById("listGO");
    listGO.onclick = function () {
        var date = document.getElementById("date").value;
        var listC = document.getElementById("listC").value;
        pageList(date, listC);
    };

    //设置分页方法 empListP
    var listP = document.getElementById("listP");
    listP.onclick = function (){
        var listC = parseInt(document.getElementById("listC").value);
        if(listC == 1){
            return;
        }
        listC = listC - 1;
        document.getElementById("listC").value = listC;
        var date = document.getElementById("date").value;
        pageList(date, listC);
    };

    //设置分页方法 empListN

    var listN = document.getElementById("listN");
    listN.onclick = function (){
        var listPageCount = parseInt(document.getElementById("listPageCount").innerText);
        var listC = parseInt(document.getElementById("listC").value);
        if(listC == listPageCount){
            return;
        }
        listC = listC + 1;
        document.getElementById("listC").value = listC;
        var date = document.getElementById("date").value;
        pageList(date, listC);
    }
}

function getReport(date, toPage){
    //分页数据
    var par = "action=apiReport";
    par = par + "&type=3";
    par = par + "&toPage=" + toPage;
    par = par + "&date=" + date;

    var results = document.getElementsByClassName("content")[0];
    results.innerText = "";
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.open("get", "/caseManager/loadReport?" + par, true);
    xmlHttp.onreadystatechange = function(){
        if (xmlHttp.readyState==4){
            if (xmlHttp.status==200){
                var r = JSON.parse(xmlHttp.response);
                if (r["apiStatus"] == "1"){
                    //页面总数
                    document.getElementById("listPageCount").innerText = r["result"]["pageCount"];

                    //加载列表
                    var listShell = document.createElement("div");
                    listShell.style.width = "80%";
                    listShell.style.marginLeft = "20px";
                    results.appendChild(listShell);
                    var resultArr = r["result"]["resultArr"];
                    for(var index in resultArr){
                        var aLine = document.createElement("div");
                        aLine.style.width = "100%";
                        aLine.style.cursor = "pointer";
                        //var name = resultArr[index][0];
                        //var isSuccess = resultArr[index][1];
                        //var createTime = resultArr[index][2];
                        //var resultId = resultArr[index][3];
                        //var logs = resultArr[index][4];
                        //var testResult = resultArr[index][5];
                        //var expectResult = resultArr[index][6];

                        var name = resultArr[index][1];
                        var isSuccess = resultArr[index][2];
                        var createTime = resultArr[index][3];
                        var resultId = resultArr[index][4];
                        var logs = "";
                        var testResult = resultArr[index][5];
                        var expectResult = resultArr[index][6];

                        var collapseHead = document.createElement("p");
                        collapseHead.innerText = "用例名：" + name + "；   测试时间：" + createTime;
                        if(isSuccess == 0){
                            collapseHead.style.color = "red";
                        }
                        //渲染折叠数据
                        collapseHead.setAttribute("data-toggle", "collapse");
                        collapseHead.setAttribute("data-target", "#rID" + resultId);
                        var detailDiv = document.createElement("div");
                        detailDiv.setAttribute("id", "rID" + resultId);
                        detailDiv.setAttribute("class", "collapse");
                        aLine.appendChild(collapseHead);
                        aLine.appendChild(detailDiv);
                        var detailUl = document.createElement("ul");

                        try{
                            var jsonLogs = JSON.parse(logs);
                            for(var ind in jsonLogs){
                                var detailLi_1 = document.createElement("li");
                                detailLi_1.innerText = "【日志】" + JSON.stringify(jsonLogs[ind]);
                                detailUl.appendChild(detailLi_1);
                            }
                        }catch (e){
                            var detailLi_j_1 = document.createElement("li");
                            detailLi_j_1.innerText = "【日志】" + logs;
                            detailUl.appendChild(detailLi_j_1);
                        }

                        try{
                            var jsontestResult = JSON.parse(testResult);
                            for(var ind2 in jsontestResult){
                                var detailLi_2 = document.createElement("li");
                                detailLi_2.innerText = "【测试结果】" + JSON.stringify(jsontestResult[ind2]);
                                detailUl.appendChild(detailLi_2);
                            }
                        }catch (e){
                            var detailLi_j_2 = document.createElement("li");
                            detailLi_j_2.innerText = "【测试结果】" + testResult;
                            detailUl.appendChild(detailLi_j_2);
                        }

                        try{
                            var jsonexpectResult = JSON.parse(expectResult);
                            for(var ind3 in jsontestResult){
                                var detailLi_3 = document.createElement("li");
                                detailLi_3.innerText = "【测试结果】" + JSON.stringify(jsonexpectResult[ind3]);
                                detailUl.appendChild(detailLi_3);
                            }
                        }catch (e){
                            var detailLi_j_3 = document.createElement("li");
                            detailLi_j_3.innerText = "【测试结果】" + expectResult;
                            detailUl.appendChild(detailLi_j_3);
                        }


                        detailDiv.appendChild(detailUl);
                        listShell.appendChild(aLine);
                    }
                }else{
                    alert(r["errorMsg"]);
                }
            }else{
                alert("服务器异常");
            }
        }
    };
    xmlHttp.send();
}